<?php

namespace app\modules\controllers;
//use app\models\Part;
use app\models\parts;
use Yii;
use yii\web\Controller;
use app\models\Follow;
use app\models\YiiUser;
use yii\data\Pagination;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;

class AutoPartsController extends Controller{
    public $layout  = 'layout';


 
    /**
     * 零配件列表页面
     * @return string 后台默认页面
     */
    public function  actionList(){

//        $model=new Part();
        $model = new parts();
	    $uid=Yii::$app->user->getId();
        $count=$model->find()->count();
        $page=new Pagination(['defaultPageSize'=>10,'totalCount'=>$count]);
        $df=$model->find()->offset($page->offset)->limit($page->limit)->all();
        return $this->render('list',['page'=>$page,'parts'=>$df]);


    }

    /**
     * 零配件编辑页面
     * @return string
     */
    public function  actionEdit( ){

//        $model=new Part();
        $model = new parts();
        $uid=Yii::$app->user->getId();

        $get = Yii::$app->request->get();
        $id = isset( $get['id'] ) ? $get['id'] : '';

//        echo Yii::$app->request->getMethod();die();
        if( Yii::$app->request->getMethod() == 'POST' ){
            $post = Yii::$app->request->post();
            $post = $post['parts'];
//            print_r( $post ); die();
            if( !empty( $id ) ){

                $model = $model->findOne( array( 'id' => $id ) );
                if( !empty( $model ) ){
                    $model->title = $post['title'];
                    $model->part_number = $post['part_number'];
                    $model->manufacturer_number = $post['manufacturer_number'];
                    $model->list_price = $post['list_price'];
                    $model->universal_part = $post['universal_part'];
                    $model->brand_title = $post['brand_title'];
                    $model->brand_id = $post['brand_id'];
                    $model->fits = $post['fits'];

                    $model->save();

                }else{
                    echo 'nothing parts';

                }

            }else{

                $model->id = time();
                $model->title = $post['title'];
                $model->part_number = $post['part_number'];
                $model->manufacturer_number = $post['manufacturer_number'];
                $model->list_price = $post['list_price'];
                $model->universal_part = $post['universal_part'];
                $model->brand_title = $post['brand_title'];
                $model->brand_id = $post['brand_id'];
                $model->fits = $post['fits'];

                $model->save();
            }


        }

        if( !empty( $model->$id ) ){
            $info = $model->findOne( array( 'id' => $id ));
        }else{
            $info = new parts();
        }


//        print_r( $info );die();

        return $this->render('edit',['info'=>$info]);



    }



}
